package com.swak.frame.hystrix;

import java.util.concurrent.Future;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.alibaba.fastjson.JSON;
import com.swak.frame.dto.request.SwakRequest;
import com.swak.frame.dto.response.RpcResponse;
import com.swak.frame.exception.SwakAssert;

/**
 * {@link @SwakHystrix} 测试隔离和熔断
 * 
 * @ClassName: HystrixDemoServiceTest.java
 * @author: colley.ma
 * @date: 2022/01/21
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@ComponentScan("com.swak.frame")
public class HystrixDemoServiceTest {

	@Resource
	private HystrixDemoService hystrixDemoService;

	@Test
	public void testHystrix() {
		SwakRequest request = new SwakRequest();
		request.setPin("colley.ma");
		RpcResponse<String> result = hystrixDemoService.hystrixDemo(request);
		System.out.println(JSON.toJSONString(result));
		SwakAssert.isTrue(result.isSuccess());
	}

	@Test
	public void testHystrixAsync() throws Exception {
		SwakRequest request = new SwakRequest();
		request.setPin("colley.ma");
		Future<RpcResponse<String>> f = hystrixDemoService.hystrixDemoAsync(request);
		RpcResponse<String> result = f.get();
		System.out.println(JSON.toJSONString(result));
		SwakAssert.isTrue(result.isSuccess());
	}
}
